package com.smartHire.controller.User.Cart;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.smartHire.domain.Cart;
import com.smartHire.service.CartService;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.lang.reflect.Array;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

@WebServlet(name = "DeleteCartServlet", value = "/DeleteCartServlet")
public class DeleteCartServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String DeleteArray= request.getParameter("DeleteArray");
        System.out.println(DeleteArray);
        WebApplicationContext ctx = null;
        //使用框架中的方法，获取容器对象
        ServletContext sc = getServletContext();
        ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(sc);
        System.out.println("容器对象的信息========"+ctx);

        try {
            CartService cartService = (CartService) ctx.getBean("cartService");
            JSONArray array = JSONObject.parseArray(DeleteArray);
            for (Object a: array)
            {
            Cart cart = new Cart();
            cart.setC_id(Integer.parseInt(a.toString()));

            if(cartService.deleteCart(cart)==1)
            {
                response.getWriter().write("OK");
            }else
            {
                response.getWriter().write("NO");
            }
                System.out.println();
            }
//



        }catch (Exception e)
        {
            e.printStackTrace();
        }


    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
